package org.pplb5.siresto;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ActivityDetilRestoran extends Activity {
	 @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        
	        if (android.os.Build.VERSION.SDK_INT > 9) {
			    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
			    StrictMode.setThreadPolicy(policy);
			}
		    	        
	        this.setContentView(R.layout.resto_view);
	 
	        TextView txtProduct = (TextView) findViewById(R.id.resto_nama);
	 
	        Intent i = getIntent();
	        final String namaResto = i.getStringExtra("NAMA_RESTO");
	        txtProduct.setText(namaResto);
	        
	        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair("namaRestoran",
					namaResto.toString()));
	        
			String res = null;
			String noResto = null;

			try {
				res = CustomHttpClient.executeHttpPost(
						"http://annisadwinidia.hostei.com/getInfoRestoran.php", postParameters);

				String result = res.toString();  

				try{
					JSONArray jArray = new JSONArray(result);
					JSONObject json = jArray.getJSONObject(0);
					noResto = json.getString("noRestoran");
				}
				catch(JSONException e){
					Log.e("log_tag", "Error parsing data "+e.toString());
				}
			}
			catch (Exception e) {
				Log.e("log_tag","Error in http connection!!" + e.toString());     
			}

			
			ArrayList<NameValuePair> postParameters2 = new ArrayList<NameValuePair>();
			postParameters2.add(new BasicNameValuePair("noRestoran",
					noResto.toString()));
			
	        ImageView logoResto = (ImageView) findViewById(R.id.resto_icon);
	        URL url;
	        Bitmap bmp;
	        String response = null;
			try {
				response = CustomHttpClient.executeHttpPost(
						"http://annisadwinidia.hostei.com/getImageResto.php", postParameters2);
				String result = response.toString();  
				JSONArray jArray = new JSONArray(result);
				JSONObject json = jArray.getJSONObject(0);
				String menu = json.getString("logo");			
				url = new URL(menu);
				bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
				logoResto.setImageBitmap(bmp);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
	        
	        Button tombolPesanAntar = (Button) findViewById(R.id.tombol_pesanantar);
	        Button tombolPaketAcara = (Button) findViewById(R.id.tombol_paketacara);
	        Button tombolReservasiTempat = (Button) findViewById(R.id.tombol_reservasitempat);
	        
	        tombolPesanAntar.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					Intent daftarMenu = new Intent(getApplicationContext(),DaftarMenu.class);
					daftarMenu.putExtra("NAMA_RESTO", namaResto);
					startActivity(daftarMenu); 
					
				}
			});
	        
	        tombolPaketAcara.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View arg0) {
					Intent paketAcara = new Intent(getApplicationContext(),DaftarPaketAcara.class);
					paketAcara.putExtra("NAMA_RESTO", namaResto);
					startActivity(paketAcara);
					
				}        	
	        });		
	        
	        tombolReservasiTempat.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View arg0) {
					//Intent reservasi = new Intent(getApplicationContext(),DaftarMejaPersonal.class);
					Intent meja = new Intent(getApplicationContext(),FormReservasi.class);
					meja.putExtra("NAMA_RESTO", namaResto);
					startActivity(meja);
				}        	
	        });
	 
	    }
}
